package com.sunfeng.a01;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.support.DefaultSingletonBeanRegistry;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.io.Resource;

import java.io.IOException;
import java.lang.reflect.Field;
import java.util.Map;

/**
 * @Author : 孙峰
 * @Description:
 * @Date : 2022/4/7  11:18
 */
@Slf4j
@SpringBootApplication
public class A01Application {
    public static void main(String[] args) throws NoSuchFieldException, IllegalAccessException, IOException {
        ConfigurableApplicationContext context = SpringApplication.run(A01Application.class, args);
        System.out.println(context);
        /**
         * BeanFactory 能干点啥
         * - 表面上只有getBean
         * - 实际上控制翻转，基本的依赖注入，直至Bean的生命周期的各种功能，都由它的实现类提供
         *
         */
        Field singletonObjects = DefaultSingletonBeanRegistry.class.getDeclaredField("singletonObjects");
        singletonObjects.setAccessible(true);
        ConfigurableListableBeanFactory beanFactory = context.getBeanFactory();
        Map<String,Object> map =(Map<String,Object>) singletonObjects.get(beanFactory);
       map.entrySet().stream().filter(e->e.getKey().startsWith("component")).forEach(e->{
           System.out.println(e.getKey()+"========"+e.getValue());
       });

       // 国际化
       // context.getMessage()

        /**
         * 通配符获取资源
         */
//        Resource[] resources = context.getResources("classpath:application.properties");
//        for (Resource resource : resources) {
//            System.out.println(resource);
//        }

        Resource[] resources = context.getResources("classpath*:META-INF/spring.factories");
        for (Resource resource : resources) {
            System.out.println(resource);
        }
    /**
     *  获取 环境变量
     */
        String java_home = context.getEnvironment().getProperty("java_home");
        System.out.println(java_home);

        context.getBean(Component1.class).register();


    }
}
